<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-${title}`"
		width="1000px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-4 flex-end-center">
			<jk-select v-model="searchParams.groupId" :option-list="groupList" class="search-bar-item-size2 item-gap" placeholder="班组" />
			<jk-search-input v-model="searchParams.nameOrCode" class="search-bar-item-size3 item-gap" placeholder="姓名或编号" @on-enter="onSearchBtn"></jk-search-input>
			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</header>

		<jk-table
			ref="jkTable"
			:radio-config="{trigger: 'row'}"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="dialogTableHeight - 10"
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column type="radio" width="60" align="center" title="单选" fixed="left"></vxe-table-column>
			<vxe-table-column field="name" title="姓名" fixed :width="100"> </vxe-table-column>
			<vxe-table-column field="code" title="编号" :width="120" />
			<vxe-table-column field="gender" title="性别" align="center" :width="70">
				<template #default="scope">
					<el-tag :type="scope.row.gender ? 'primary' : 'success'" size="mini">{{ scope.row.gender ? '男' : '女' }}</el-tag>
				</template>
			</vxe-table-column>
			<vxe-table-column field="postName" title="岗位" min-width="100" />
			<vxe-table-column field="groupName" title="班组" min-width="80" />
			<vxe-table-column field="workshopName" title="区域" :width="100" />
			<vxe-table-column field="leaderName" title="直接上级" min-width="90" />
			<vxe-table-column field="deptName" title="所属部门" min-width="100" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../mixins/cal-dialog-height';
    import { pageConfig } from '../../../../utils/jk/jk-tool';
    import { getEmpPage } from '../../../../api/system/emp';
    import { personnelGroupPage } from '../../../../api/personnel/group';

    export default {
        name: 'SelectProductionDialog',
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: '人员'
            }
        },
        data() {
            return {
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    workshopId: null,
                    nameOrCode: '',
                    groupId: null
                },
                groupList: [],
                workshopList: [],
                selectRow: null,
                searchBarProcessId: null,
                searchBarNameOrCode: '',
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: []
            };
        },
        methods: {
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectData = selection.row;
            },
            checkboxMethod({ row }) {
                return row._disabled !== true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (Object.keys(this.selectData).length) {
                    this.$emit('on-confirm', this.selectData);
                } else {
                    this.$tipsBar(this, 'unChoose');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                this.selectData = [];
                return getEmpPage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.tableData = res.data.res;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getGroupListRequest() {
                return personnelGroupPage({
                    auditState: 1
                }).then(res => {
                    if (res.data.status === 200) {
                        this.groupList = res.data.res.filter(x => {
                            return [59, 61, 62].includes(x.id);
                        }) || [];
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                if (!this.searchParams.workshopId) {
                    this.workshopList.forEach(x => {
                        if (x.code === 'QF') {
                            this.$set(this.searchParams, 'workshopId', x.id);
                        }
                    });
                }
                await this.getGroupListRequest();
                if (!this.searchParams.groupId) {
                    this.groupList.forEach(x => {
                        if (x.code === 'jiaban' && x.workshopId === this.searchParams.workshopId) {
                            this.$set(this.searchParams, 'groupId', x.id);
                        }
                    });
                }
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
